<?php
/**
 * 騰訊開發平臺
 * 
 * */

$apiparams = array (
		'appid' => '100657804',
		'appkey' => '9c6b43553601f7b9e8d34d24677e0042',
		//'server_name' => '119.147.19.43'  // 测试期使用这个IP
        'server_name' => 'openapi.tencentyun.com' //正式环境使用这个
);
require_once LIB_PATH . '/openqq/PHP_SDK_V3.0.4/OpenApiV3.php';
class myopenqqv3 extends OpenApiV3 {
	// 获取uid
	public function getUser($db, $openid, $openkey, $pf) {
		$params = array (
				'openid' => $openid,
				'openkey' => $openkey,
				'pf' => $pf 
		);
		if (in_array($pf, array("3366", "qqgame")))
		{
			$params['charset'] = "utf-8";
		}
		$script_name = '/v3/user/get_info';
		$userinfo = $this->api ( $script_name, $params );
		if ($userinfo ['ret'] != 0 || $openid == 'empty_openid') { // 测试
			$userinfo ['ret'] = 0;
			$userinfo ['nickname']  = $_SERVER['REMOTE_ADDR'];
			$userinfo ['figureurl'] = 'http://www.qilaiwan.com/images/logo60.png';
		}
		$result = false;
		if (0 == $userinfo ['ret']) {			
			$sql = "SELECT * FROM user WHERE openid = '" . $openid . "'";
			$result = $db->getOne ( $sql );
			if (empty ( $result )) {
				// 新用户要记录到user表里
				$result ['openid'] = $openid;
				$result ['nickname'] = $userinfo ['nickname'];
				$result ['cip'] = get_user_ip ();
				$result ['ctime'] = date ( "Y-m-d H:i:s", time () );
				$result ['figureurl'] = $userinfo ['figureurl'];
				$db->insert ( 'user', $result );
				$result ['uid'] = $db->getInsertId ();
			}
			//增加用户黄钻信息
			$result['is_yellow_vip'] = $userinfo['is_yellow_vip'];
			$result['is_yellow_year_vip'] = $userinfo['is_yellow_year_vip'];
			$result['yellow_vip_level'] = $userinfo['yellow_vip_level'];

			//增加用户蓝钻信息（用于3366平台）
			$result['is_blue_vip'] = $userinfo['is_blue_vip'];   //是否为蓝钻用户（0：不是； 1：是）
			$result['is_blue_year_vip'] = $userinfo['is_blue_year_vip'];  //是否为年费蓝钻用户（0：不是； 1：是）
			$result['blue_vip_level'] = $userinfo['blue_vip_level'];  //蓝钻等级（如果是蓝钻用户才返回此参数）
			$result['3366_level'] = $userinfo['3366_level'];    //3366用户的大等级。（当pf=3366时返回）
			$result['3366_level_name'] = $userinfo['3366_level_name'];     //3366用户的等级名，如小游游、小游仙
			$result['3366_grow_level'] = $userinfo['3366_grow_level'];      //3366用户的成长等级
			$result['3366_grow_value'] = $userinfo['3366_grow_value'];    //3366用户的成长值
		}
		//更新用户的开放平台等级信息
		set_user_openlevel($db, $result);
		return $result;
	}
}
/**
 * 获取好友资料
 *
 * @param object $sdk
 *        	OpenApiV3 Object
 * @param string $openid
 *        	openid
 * @param string $openkey
 *        	openkey
 * @param string $pf
 *        	平台
 * @return array 好友资料数组
 */
function get_user_info($sdk, $openid, $openkey, $pf) {
	$params = array (
			'openid' => $openid,
			'openkey' => $openkey,
			'pf' => $pf 
	);
	
	$script_name = '/v3/user/get_info';
	
	return $sdk->api ( $script_name, $params );
}

/**
 * 获取交易token
 */
function buy_goods($sdk, $params = array()) {
	if (empty ( $params )) {
		return false;
	}
	$script_name = '/v3/pay/buy_goods';
	
	return $sdk->api ( $script_name, $params, 'post', 'https' );
}

/**
 * 向腾讯确认发货结果
 */
function confirm_delivery($sdk, $params = array()) {
	if (empty ( $params )) {
		return false;
	}
	$script_name = '/v3/pay/confirm_delivery';
	
	return $sdk->api ( $script_name, $params );
}

/**
 * 获取黄钻每日礼包token
 */

function yellowgift($sdk, $params = array()){

	if (empty($params)){
	
	return false;
	
}

	$script_name = '/v3/pay/get_token';
	return $sdk->api($script_name, $params);

}


/**
 * 获取用户ip
 */
function get_user_ip() {
	if ($HTTP_SERVER_VARS ["HTTP_X_FORWARDED_FOR"]) {
		$ip = $HTTP_SERVER_VARS ["HTTP_X_FORWARDED_FOR"];
	} elseif ($HTTP_SERVER_VARS ["HTTP_CLIENT_IP"]) {
		$ip = $HTTP_SERVER_VARS ["HTTP_CLIENT_IP"];
	} elseif ($HTTP_SERVER_VARS ["REMOTE_ADDR"]) {
		$ip = $HTTP_SERVER_VARS ["REMOTE_ADDR"];
	} elseif (getenv ( "HTTP_X_FORWARDED_FOR" )) {
		$ip = getenv ( "HTTP_X_FORWARDED_FOR" );
	} elseif (getenv ( "HTTP_CLIENT_IP" )) {
		$ip = getenv ( "HTTP_CLIENT_IP" );
	} elseif (getenv ( "REMOTE_ADDR" )) {
		$ip = getenv ( "REMOTE_ADDR" );
	} else {
		$ip = "Unknown";
	}
	return $ip;
}

/**
 * 更新用户黄钻、蓝钻、3366等级
 */
function set_user_openlevel($db, $user_openinfo){
	if (empty($user_openinfo['uid'])){
		return false;
	}
	$openinfo = array();
	$sql_1 = "select * from user_openinfo where uid = '" . $user_openinfo['uid'] ."'";
	$result = $db->getOne ( $sql_1 );
	if (empty($result)){
		//第一次登录的用户直接写入一条新纪录
		$openinfo['uid'] = $user_openinfo['uid'];
		$openinfo['is_yellow_vip'] = $user_openinfo['is_yellow_vip'];
		$openinfo['is_yellow_year_vip'] = $user_openinfo['is_yellow_year_vip'];
		$openinfo['yellow_vip_level'] = $user_openinfo['yellow_vip_level'];
		$openinfo['is_blue_vip'] = $user_openinfo['is_blue_vip'];
		$openinfo['is_blue_year_vip'] = $user_openinfo['is_blue_year_vip'];
		$openinfo['blue_vip_level'] = $user_openinfo['blue_vip_level'];
		$openinfo['3366_level'] = $user_openinfo['3366_level'];
		$openinfo['3366_level_name'] = $user_openinfo['3366_level_name'];
		$openinfo['3366_grow_level'] = $user_openinfo['3366_grow_level'];
		$openinfo['3366_grow_value'] = $user_openinfo['3366_grow_value'];
		$openinfo['uptime'] = date ( 'Y-m-d H:i:s' );
		$db->insert ( 'user_openinfo', $openinfo );
	}else{
		//老用户检查等级数据有无变化
		$set_text = "";
		foreach ($result as $k => $v){
			if (is_null($user_openinfo[$k]) == false && ($user_openinfo[$k] !== $v) && $k!="uptime"){
				$set_text .= "," . $k . "='" . $user_openinfo[$k] . "'";
			}
		}
		if (!empty($set_text)){
			$set_text = substr($set_text, 1) . ",uptime='" . date ( 'Y-m-d H:i:s' ) . "'";
			$sql_2 = "update user_openinfo set ".$set_text." where uid='".$user_openinfo['uid']."'";
			$db->query ( $sql_2 );
		}
	}
}